<?php

namespace app\admin\model\scanwork;

use think\Model;

/**
 * 采购记录模型
 */
class PurchaseRecord extends Model
{
    // 表名
    protected $name = 'scanwork_purchase_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'payment_status_text',
        'arrival_status_text',
        'quality_status_text'
    ];
    
    /**
     * 付款状态列表
     */
    public function getPaymentStatusList()
    {
        return [
            '0' => __('未付款'),
            '1' => __('部分付款'),
            '2' => __('已付清')
        ];
    }

    /**
     * 付款状态文本
     */
    public function getPaymentStatusTextAttr($value, $data)
    {
        $value = isset($data['payment_status']) ? $data['payment_status'] : 0;
        $list = $this->getPaymentStatusList();
        $text = isset($list[$value]) ? $list[$value] : '';
        
        $colorMap = ['0' => 'danger', '1' => 'warning', '2' => 'success'];
        $color = isset($colorMap[$value]) ? $colorMap[$value] : 'default';
        
        return '<span class="label label-' . $color . '">' . $text . '</span>';
    }

    /**
     * 到货状态列表
     */
    public function getArrivalStatusList()
    {
        return [
            '0' => __('未到货'),
            '1' => __('部分到货'),
            '2' => __('已到货')
        ];
    }

    /**
     * 到货状态文本
     */
    public function getArrivalStatusTextAttr($value, $data)
    {
        $value = isset($data['arrival_status']) ? $data['arrival_status'] : 0;
        $list = $this->getArrivalStatusList();
        $text = isset($list[$value]) ? $list[$value] : '';
        
        $colorMap = ['0' => 'default', '1' => 'warning', '2' => 'success'];
        $color = isset($colorMap[$value]) ? $colorMap[$value] : 'default';
        
        return '<span class="label label-' . $color . '">' . $text . '</span>';
    }

    /**
     * 质检状态列表
     */
    public function getQualityStatusList()
    {
        return [
            '0' => __('待检'),
            '1' => __('合格'),
            '2' => __('不合格')
        ];
    }

    /**
     * 质检状态文本
     */
    public function getQualityStatusTextAttr($value, $data)
    {
        $value = isset($data['quality_status']) ? $data['quality_status'] : 0;
        $list = $this->getQualityStatusList();
        $text = isset($list[$value]) ? $list[$value] : '';
        
        $colorMap = ['0' => 'default', '1' => 'success', '2' => 'danger'];
        $color = isset($colorMap[$value]) ? $colorMap[$value] : 'default';
        
        return '<span class="label label-' . $color . '">' . $text . '</span>';
    }

    /**
     * 关联订单
     */
    public function order()
    {
        return $this->belongsTo('Order', 'order_id', 'id');
    }

    /**
     * 关联供应商
     */
    public function supplier()
    {
        return $this->belongsTo('Supplier', 'supplier_id', 'id');
    }

    /**
     * 关联物料
     */
    public function material()
    {
        return $this->belongsTo('Material', 'material_id', 'id');
    }

    /**
     * 关联采购员
     */
    public function buyer()
    {
        return $this->belongsTo('app\admin\model\Admin', 'buyer_id', 'id');
    }

    /**
     * 检查是否延期
     */
    public function isDelayed()
    {
        if ($this->arrival_status == 2) {
            return false;
        }
        
        $now = time();
        $expectedDate = $this->expected_arrival_date ? $this->expected_arrival_date : 0;
        
        return $now > $expectedDate;
    }

    /**
     * 计算延期天数
     */
    public function getDelayDaysAttr($value, $data)
    {
        if (!$this->isDelayed()) {
            return 0;
        }
        
        $now = time();
        $expectedDate = $this->expected_arrival_date;
        
        return ceil(($now - $expectedDate) / 86400);
    }
}

